﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// Enumerable扩展类
    /// </summary>
    unsafe public partial class EnumerableEx
    {
        #region 指针数据最大值求取扩展

        /// <summary>
        /// 返回byte序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static byte Max(byte* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回sbyte序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static sbyte Max(sbyte* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回ushort序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static ushort Max(ushort* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回short序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static short Max(short* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }


        /// <summary>
        /// 返回int序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static int Max(int* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回uint序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static uint Max(uint* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回long序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static long Max(long* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回ulong序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static ulong Max(ulong* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回float序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static float Max(float* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回double序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static double Max(double* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }

        /// <summary>
        /// 返回decimal序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static decimal Max(decimal* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (max < sourcePtr[i])
                {
                    max = sourcePtr[i];
                }
            }
            return max;
        }


        #endregion


        #region 最大值求取扩展

        /// <summary>
        /// 返回byte序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static byte Max(IEnumerable<byte> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回sbyte序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static sbyte Max(IEnumerable<sbyte> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回ushort序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static ushort Max(IEnumerable<ushort> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回short序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static short Max(IEnumerable<short> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }


        /// <summary>
        /// 返回int序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static int Max(IEnumerable<int> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回uint序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static uint Max(IEnumerable<uint> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回long序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static long Max(IEnumerable<long> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回ulong序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static ulong Max(IEnumerable<ulong> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回float序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static float Max(IEnumerable<float> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回double序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static double Max(IEnumerable<double> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回decimal序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static decimal Max(IEnumerable<decimal> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回BigInteger序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static BigInteger Max(IEnumerable<BigInteger> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

#if CORE7_P
        /// <summary>
        /// 返回Int128序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static Int128 Max(IEnumerable<Int128> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回UInt128序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static UInt128 Max(IEnumerable<UInt128> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }
#endif

#if CORE
        /// <summary>
        /// 返回Half序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static Half Max(IEnumerable<Half> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回DateOnly序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static DateOnly Max(IEnumerable<DateOnly> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回TimeOnly序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static TimeOnly Max(IEnumerable<TimeOnly> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }
#endif
        /// <summary>
        /// 返回DateTime序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static DateTime Max(IEnumerable<DateTime> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回DateTimeOffset序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static DateTimeOffset Max(IEnumerable<DateTimeOffset> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回TimeSpan序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static TimeSpan Max(IEnumerable<TimeSpan> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (max < source.ElementAt(i))
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }





        /// <summary>
        /// 返回序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最大值</returns>
        public static TSource Max<TSource>(IEnumerable<TSource> source, int startIndex, int count)
            where TSource : IComparable
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var max = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i).CompareTo(max) > 0)
                {
                    max = source.ElementAt(i);
                }
            }
            return max;
        }

        /// <summary>
        /// 返回序列中指定位置以及指定元素个数中数据最大值
        /// </summary>
        /// <typeparam name="TSource">源数据类型</typeparam>
        /// <typeparam name="TResult">目标数据类型</typeparam>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="selector">数据获取器</param>
        /// <returns>最大值</returns>
        public static TResult Max<TSource, TResult>(IEnumerable<TSource> source, int startIndex, int count, Func<TSource, TResult> selector)
            where TResult : IComparable
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            TResult max = selector(source.ElementAt(startIndex));
            TResult tmp;

            for (int i = startIndex + 1; i < end; i++)
            {
                tmp = selector(source.ElementAt(i));
                if (tmp.CompareTo(max) > 0)
                {
                    max = tmp;
                }
            }
            return max;
        }

        #endregion



        //****************************************************************************************************
        //****************************************************************************************************


        #region 指针数据最小值求取扩展

        /// <summary>
        /// 返回byte序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static byte Min(byte* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回sbyte序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static sbyte Min(sbyte* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回short序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static short Min(short* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回ushort序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static ushort Min(ushort* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回int序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static int Min(int* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回uint序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static uint Min(uint* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回long序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static long Min(long* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回ulong序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static ulong Min(ulong* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回float序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static float Min(float* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回double序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static double Min(double* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        /// <summary>
        /// 返回decimal序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static decimal Min(decimal* sourcePtr, int count)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = sourcePtr[0];
            for (int i = +1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }
            }
            return min;
        }

        #endregion


        #region 最小值求取扩展

        /// <summary>
        /// 返回byte序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static byte Min(IEnumerable<byte> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回sbyte序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static sbyte Min(IEnumerable<sbyte> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回short序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static short Min(IEnumerable<short> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回ushort序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static ushort Min(IEnumerable<ushort> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回int序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static int Min(IEnumerable<int> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回uint序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static uint Min(IEnumerable<uint> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回long序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static long Min(IEnumerable<long> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回ulong序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static ulong Min(IEnumerable<ulong> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回float序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static float Min(IEnumerable<float> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回double序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static double Min(IEnumerable<double> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回decimal序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static decimal Min(IEnumerable<decimal> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回BigInteger序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static BigInteger Min(IEnumerable<BigInteger> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

#if CORE7_P
        /// <summary>
        /// 返回Int128序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static Int128 Min(IEnumerable<Int128> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回UInt128序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static UInt128 Min(IEnumerable<UInt128> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }
#endif

#if CORE
        /// <summary>
        /// 返回Half序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static Half Min(IEnumerable<Half> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回DateOnly序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static DateOnly Min(IEnumerable<DateOnly> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回TimeOnly序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static TimeOnly Min(IEnumerable<TimeOnly> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }
#endif
        /// <summary>
        /// 返回DateTime序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static DateTime Min(IEnumerable<DateTime> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回DateTimeOffset序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static DateTimeOffset Min(IEnumerable<DateTimeOffset> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回TimeSpan序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static TimeSpan Min(IEnumerable<TimeSpan> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            var min = source.ElementAt(startIndex);
            int end = startIndex + count;
            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i) < min)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }



        /// <summary>
        /// 返回序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>最小值</returns>
        public static TSource Min<TSource>(IEnumerable<TSource> source, int startIndex, int count)
            where TSource : IComparable
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            TSource min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                if (source.ElementAt(i).CompareTo(min) < 0)
                {
                    min = source.ElementAt(i);
                }
            }
            return min;
        }

        /// <summary>
        /// 返回序列中指定位置以及指定元素个数中数据最小值
        /// </summary>
        /// <typeparam name="TSource">源数据类型</typeparam>
        /// <typeparam name="TResult">目标数据类型</typeparam>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="selector">数据获取器</param>
        /// <returns>最小值</returns>
        public static TResult Min<TSource, TResult>(IEnumerable<TSource> source, int startIndex, int count, Func<TSource, TResult> selector)
            where TResult : IComparable
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            TResult min = selector(source.ElementAt(startIndex));
            TResult tmp;

            for (int i = startIndex + 1; i < end; i++)
            {
                tmp = selector(source.ElementAt(i));
                if (tmp.CompareTo(min) < 0)
                {
                    min = tmp;
                }
            }
            return min;
        }

        #endregion



        //****************************************************************************************************
        //****************************************************************************************************



        #region 指针数据最大最小值求取扩展


        /// <summary>
        /// 返回byte序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(byte* sourcePtr, int count, out byte min, out byte max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回sbyte序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(sbyte* sourcePtr, int count, out sbyte min, out sbyte max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回short序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(short* sourcePtr, int count, out short min, out short max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回ushort序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(ushort* sourcePtr, int count, out ushort min, out ushort max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回int序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(int* sourcePtr, int count, out int min, out int max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回uint序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(uint* sourcePtr, int count, out uint min, out uint max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回long序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(long* sourcePtr, int count, out long min, out long max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回ulong序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(ulong* sourcePtr, int count, out ulong min, out ulong max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回float序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(float* sourcePtr, int count, out float min, out float max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回double序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(double* sourcePtr, int count, out double min, out double max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        /// <summary>
        /// 返回decimal序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="sourcePtr">序列</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(decimal* sourcePtr, int count, out decimal min, out decimal max)
        {
            if (sourcePtr == null)
            {
                throw new ArgumentNullException(nameof(sourcePtr));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            max = min = sourcePtr[0];
            for (int i = 1; i < count; i++)
            {
                if (sourcePtr[i] < min)
                {
                    min = sourcePtr[i];
                }

                if (sourcePtr[i] > max)
                {
                    max = sourcePtr[i];
                }
            }
        }

        #endregion



        #region 最大最小值求取扩展


        /// <summary>
        /// 返回byte序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<byte> source, int startIndex,
            int count, out byte min, out byte max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            byte temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回sbyte序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<sbyte> source, int startIndex,
            int count, out sbyte min, out sbyte max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            sbyte temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回short序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<short> source, int startIndex,
            int count, out short min, out short max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            short temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回ushort序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<ushort> source, int startIndex,
            int count, out ushort min, out ushort max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            ushort temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回int序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<int> source, int startIndex,
            int count, out int min, out int max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回uint序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<uint> source, int startIndex,
            int count, out uint min, out uint max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            uint temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回long序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<long> source, int startIndex,
            int count, out long min, out long max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            long temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回ulong序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<ulong> source, int startIndex,
            int count, out ulong min, out ulong max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            ulong temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回float序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<float> source, int startIndex,
            int count, out float min, out float max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            float temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回double序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<double> source, int startIndex,
            int count, out double min, out double max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回decimal序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<decimal> source, int startIndex,
            int count, out decimal min, out decimal max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            decimal temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回BigInteger序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<BigInteger> source, int startIndex,
            int count, out BigInteger min, out BigInteger max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            BigInteger temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

#if CORE7_P
        /// <summary>
        /// 返回Int128序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<Int128> source, int startIndex,
            int count, out Int128 min, out Int128 max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            Int128 temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回UInt128序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<UInt128> source, int startIndex,
            int count, out UInt128 min, out UInt128 max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            UInt128 temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }
#endif

#if CORE
        /// <summary>
        /// 返回Half序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<Half> source, int startIndex,
            int count, out Half min, out Half max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            Half temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回DateOnly序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<DateOnly> source, int startIndex,
            int count, out DateOnly min, out DateOnly max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            DateOnly temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回TimeOnly序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<TimeOnly> source, int startIndex,
            int count, out TimeOnly min, out TimeOnly max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            TimeOnly temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }
#endif
        /// <summary>
        /// 返回DateTime序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<DateTime> source, int startIndex,
            int count, out DateTime min, out DateTime max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            DateTime temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回DateTimeOffset序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<DateTimeOffset> source, int startIndex,
            int count, out DateTimeOffset min, out DateTimeOffset max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            DateTimeOffset temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回TimeSpan序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax(IEnumerable<TimeSpan> source, int startIndex,
            int count, out TimeSpan min, out TimeSpan max)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            TimeSpan temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp < min)
                {
                    min = temp;
                }

                if (temp > max)
                {
                    max = temp;
                }
            }
        }



        /// <summary>
        /// 返回序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax<TSource>(IEnumerable<TSource> source, int startIndex,
            int count, out TSource min, out TSource max)
            where TSource : IComparable
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            TSource temp;
            int end = startIndex + count;
            max = min = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = source.ElementAt(i);

                if (temp.CompareTo(min) < 0)
                {
                    min = temp;
                }

                if (temp.CompareTo(max) > 0)
                {
                    max = temp;
                }
            }
        }

        /// <summary>
        /// 返回序列中指定位置以及指定元素个数中数据最小值和最大值
        /// </summary>
        /// <typeparam name="TSource">源数据类型</typeparam>
        /// <typeparam name="TResult">目标数据类型</typeparam>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="selector">数据获取器</param>
        /// <param name="min">输出的最小值</param>
        /// <param name="max">输出的最大值</param>
        public static void MinMax<TSource, TResult>(IEnumerable<TSource> source, int startIndex,
            int count, Func<TSource, TResult> selector, out TResult min, out TResult max)
            where TResult : IComparable
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            TResult temp;
            int end = startIndex + count;
            max = min = selector(source.ElementAt(startIndex));

            for (int i = startIndex + 1; i < end; i++)
            {
                temp = selector(source.ElementAt(i));

                if (temp.CompareTo(min) < 0)
                {
                    min = temp;
                }

                if (temp.CompareTo(max) > 0)
                {
                    max = temp;
                }
            }
        }


        #endregion


        //****************************************************************************************************
        //****************************************************************************************************



        #region 平均值求取指针扩展

        /// <summary>
        /// 计算byte序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(byte* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算sbyte序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(sbyte* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算short序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(short* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算ushort序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(ushort* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算int序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(int* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算uint序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(uint* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算long序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(long* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算ulong序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(ulong* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算float序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(float* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算double序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(double* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            double total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算decimal序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static decimal Average(decimal* source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            decimal total = source[0];
            checked
            {
                for (int i = 1; i < count; i++)
                {
                    total += source[i];
                }
            }

            return total / count;
        }

        #endregion


        #region 平均值求取扩展
        /// <summary>
        /// 计算byte序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<byte> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算sbyte序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<sbyte> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算short序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<short> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算ushort序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<ushort> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算int序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<int> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算uint序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<uint> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算long序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<long> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算ulong序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<ulong> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算float序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<float> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算double序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<double> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算decimal序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static decimal Average(IEnumerable<decimal> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            decimal total = source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += source.ElementAt(i);
                }
            }

            return total / count;
        }

        /// <summary>
        /// 计算BigInteger序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static BigInteger Average(IEnumerable<BigInteger> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            BigInteger total = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                total += source.ElementAt(i);
            }
            return (total / count);
        }

#if CORE7_P

        /// <summary>
        /// 计算Int128序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static Int128 Average(IEnumerable<Int128> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            BigInteger total = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                total += source.ElementAt(i);
            }
            return (Int128)(total / count);
        }

        /// <summary>
        /// 计算UInt128序列中指定位置以及指定元素个数的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static UInt128 Average(IEnumerable<UInt128> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            BigInteger total = source.ElementAt(startIndex);

            for (int i = startIndex + 1; i < end; i++)
            {
                total += source.ElementAt(i);
            }
            return (UInt128)(total / count);
        }

#endif

#if CORE

        /// <summary>
        /// 计算Half序列中指定位置以及指定元素个数2的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <returns>平均值</returns>
        public static double Average(IEnumerable<Half> source, int startIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = (double)source.ElementAt(startIndex);
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += (double)source.ElementAt(i);
                }
            }

            return total / count;
        }

#endif

        /// <summary>
        /// 计算Half序列中指定位置以及指定元素个数2的平均值
        /// </summary>
        /// <param name="source">序列</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素个数</param>
        /// <param name="selector">数据获取器</param>
        /// <returns>平均值</returns>
        public static double Average<TSource>(IEnumerable<TSource> source, int startIndex, int count, Func<TSource, double> selector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("没有元素");
            }

            int end = startIndex + count;
            double total = selector(source.ElementAt(startIndex));
            checked
            {
                for (int i = startIndex + 1; i < end; i++)
                {
                    total += selector(source.ElementAt(i));
                }
            }

            return total / count;
        }

        #endregion

    }
}
